package com.miyake.loveviewweb.server;

import java.util.List;
import java.util.Map;

import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.Key;
import com.miyake.loveviewweb.shared.CsvConverter;
import com.miyake.loveviewweb.shared.ArrayThinner;
import com.miyake.loveviewweb.shared.model.ChartModel;
import com.miyake.loveviewweb.shared.model.DataColumnBody;
import com.miyake.loveviewweb.shared.model.DataColumnModel;
import com.miyake.loveviewweb.shared.model.DataModel;
import com.miyake.loveviewweb.shared.model.LvBlob;
import com.miyake.loveviewweb.shared.model.PluginModel;
import com.miyake.loveviewweb.shared.model.XyDataBodyModel;
import com.miyake.rest.ChartServiceProperty;
import com.miyake.rest.DataColumn;
import com.miyake.rest.comm.XmlHttpPost;

public class ChartModelManager {
    
    public ChartModel store(ChartModel chartModel, Key dataKey, Map<String, String> axisDefine) {
        DataModel data = Datastore.get(DataModel.class, dataKey);
        Key key = Datastore.allocateId(dataKey, ChartModel.class);
        
        chartModel.setKey(key);
        
        XyDataBodyModel xyDataBody = Datastore.get(XyDataBodyModel.class, data.getDataBody());
        List<DataColumnModel> list = Datastore.get(DataColumnModel.class, xyDataBody.getDataColumnKeys());
        chartModel.setParentKey(dataKey);
        
        for (String header : axisDefine.keySet()) {  
            String s = axisDefine.get(header);
            for (DataColumnModel dataColumnModel : list) {
                if (dataColumnModel.getHeader().equals(s)) {
                    chartModel.getAxes().put(header, dataColumnModel.getKey());
                    continue;
                }
            }
        }
        
        Datastore.put(chartModel);
        return chartModel;
    }
       
    public ChartModel fillChartModel(ChartModel chartModel, boolean content) {
        if (content) {
            if (chartModel.getChartEngine().equals(ChartModel.ChartEngine.PLUGIN_CHART)) {
                if (chartModel.getChartPluginKey() == null) {
                    return chartModel;
                }
                PluginModel model = Datastore.get(PluginModel.class, chartModel.getChartPluginKey());
                final ChartServiceProperty prop = new ChartServiceProperty();
               
                prop.setHeight(chartModel.getChartHeight());
                prop.setWidth(chartModel.getChartWidth());
                prop.setTitle(chartModel.getName());

  
                for (String axisHeader : chartModel.getAxes().keySet()) {
                    Key key = chartModel.getAxes().get(axisHeader);
                    DataColumnModel col = Datastore.get(DataColumnModel.class, key);
                    String[] arr = Datastore.get(DataColumnBody.class, col.getBodyKey()).getValue();
                    arr = new ArrayThinner().thin(arr, chartModel.getChartWidth());
                    prop.getAxes().put(axisHeader, CsvConverter.get(arr));
                }
                                
                XmlHttpPost post = new XmlHttpPost(model.getServiceUrl(), prop.toXML());
                
                byte[] ret = post.getReceivedData();

                if (ret == null) {
                    System.err.println("return from chart servie is null");
                }
                
                // stores image
                LvBlob blob = new LvBlob();
                Key blobKey = Datastore.allocateId(chartModel.getKey(), LvBlob.class);
                blob.setKey(blobKey);
                blob.setByteData(ret);
                blob.setContentType(post.getResponseContentType());
                blob.setChartKey(chartModel.getKey());
                Datastore.put(blob);
                
                // size is added ito avoid 
                chartModel.setChartURL("/staticChart?key=" + Datastore.keyToString(chartModel.getKey()) + "&size=" + String.valueOf(ret.length));
                chartModel.setXarray(null);
                chartModel.setYarray(null);
            }
            else {
/*                if (chartModel.getChartDataType().equals(ChartModel.ChartDataType.XY_DATA)) {
                    
                    DataColumnModel xcol  = Datastore.get(DataColumnModel.class, chartModel.getXaxis());
                    DataColumnModel ycol  = Datastore.get(DataColumnModel.class, chartModel.getYaxis());
                    
                    double[] x = Datastore.get(DataColumnBody.class, xcol.getBodyKey()).getDoubleValue();
                    double[] y = Datastore.get(DataColumnBody.class, ycol.getBodyKey()).getDoubleValue();
            
                    x = new ArrayThinner().thin(x, chartModel.getChartWidth());
                    y = new ArrayThinner().thin(y, chartModel.getChartWidth());
                    
                    chartModel.setXarray(x);
                    chartModel.setYarray(y);
                    chartModel.setxLabel(xcol.getHeader());
                    chartModel.setyLabel(ycol.getHeader());
                }
                else if (chartModel.getChartDataType().equals(ChartModel.ChartDataType.Y_DATA)) {
                    DataColumnModel ycol  = Datastore.get(DataColumnModel.class, chartModel.getYaxis());
                    
                    double[] y = Datastore.get(DataColumnBody.class, ycol.getBodyKey()).getDoubleValue();
                    y = new ArrayThinner().thin(y, chartModel.getChartWidth());
                    chartModel.setyLabel(ycol.getHeader());
                    chartModel.setYarray(y);
                }*/
            }
            
        }
        return chartModel;
    }
    

}
